/**
 *
 * author ThuanLT
 * Aug 20, 2014 9:52:33 PM
 *
 */
package vn.fsoft.gst.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import vn.fsoft.gst.domain.Address;

@Repository
public class AddressDAOImpl implements AddressDAO {
	@Autowired(required=true)
	private SessionFactory sessionFactory;
	@Transactional
	public int addAddress(Address addres) {
		return (Integer) sessionFactory.getCurrentSession().save(addres);
	}
	@Transactional
	public void updateAddress(Address newAddress) {
		sessionFactory.getCurrentSession().merge(newAddress);
	}

	public void deleteAddress(int addressID) {
		Address add = getAddressByID(addressID);
		sessionFactory.getCurrentSession().delete(add);
	}
	@SuppressWarnings("unchecked")
	@Transactional
	public List<Address> getListAdress() {
		return sessionFactory.getCurrentSession().createQuery("from Address").list();
	}
	@Transactional
	public Address getAddressByID(int addressID) {
		return (Address) sessionFactory.getCurrentSession().get(Address.class, addressID);
	}
}
